package org.mspring.mlog.web.module.admin;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.Link;
import org.mspring.mlog.service.LinkService;
import org.mspring.mlog.service.LinkTypeService;
import org.mspring.mlog.web.support.log.Log;
import org.mspring.platform.dao.Order.OrderType;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.query.QueryBuilder;
import org.mspring.platform.utils.ArrayUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Gao Youbo
 * @since 2012-7-30
 */
@Widget
@RequestMapping("/admin/link")
public class Admin_LinkWidget extends Admin_AbstractWidget {

    @Autowired
    private LinkService linkService;
    @Autowired
    private LinkTypeService linkTypeService;

    @RequestMapping("/list")
    @Log
    public String listLinks(@ModelAttribute Page<Link> linkPage, @ModelAttribute Link link, @RequestParam Map<String, Object> queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (linkPage == null) {
            linkPage = new Page<>();
        }
        linkPage.addOrder("id", OrderType.DESC);
        linkPage.addOrder("sort", OrderType.ASC);
        linkPage.addOrder("visable", OrderType.DESC);
        QueryBuilder queryBuilder = QueryBuilder.instance(Link.class, queryParams).buildEqual("visable", "visable", boolean.class).buildLike("name", "name").buildLike("url", "url").buildEqual("type.id", "type.id", long.class);
        linkPage = linkService.listPage(queryBuilder, linkPage);

        model.addAttribute("linkPage", linkPage);
        model.addAttribute("visable", Link.Visable.getVisableMap());
        model.addAttribute("types", linkTypeService.listVisables());
        return "/admin/link/listLink";
    }

    @RequestMapping("/create")
    public String createLinkView(@ModelAttribute Link link, HttpServletRequest request, HttpServletResponse response, Model model) {
        model.addAttribute("target", Link.Target.getTargetMap());
        model.addAttribute("visable", Link.Visable.getVisableMap());
        model.addAttribute("types", linkTypeService.listVisables());
        return "/admin/link/createLink";
    }

    @RequestMapping("/create/save")
    @Log
    public String createLink(@ModelAttribute Link link, HttpServletRequest request, HttpServletResponse response, Model model) {
        linkService.insert(link);
        return "redirect:/admin/link/list";
    }

    @RequestMapping("/delete")
    @Log
    public String deleteLink(@RequestParam(required = false) Long[] id, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id != null && id.length > 0) {
            linkService.delete(id);
        }
        return "redirect:/admin/link/list";
    }

    @RequestMapping("/edit")
    public String editLinkView(@ModelAttribute Link link, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (link == null || link.getId() == null) {
            Object obj = getSessionAttribute(request, "LinkWidget_edit_id");
            if (obj != null) {
                Long id = (Long) obj;
                link = new Link(id);
            }
        }
        setSessionAttribute(request, "LinkWidget_edit_id", link.getId());
        link = linkService.get(link.getId());
        if (link == null) {
            return prompt(model, "请先选择要修改的链接");
        }
        model.addAttribute("link", link);
        model.addAttribute("target", Link.Target.getTargetMap());
        model.addAttribute("visable", Link.Visable.getVisableMap());
        model.addAttribute("types", linkTypeService.listVisables());
        return "/admin/link/editLink";
    }

    @RequestMapping("/edit/save")
    @Log
    public String edit_save(@ModelAttribute Link link, HttpServletRequest request, HttpServletResponse response, Model model) {
        linkService.update(link);
        return "redirect:/admin/link/list";
    }

    @RequestMapping("/ctrl")
    @Log
    public String ctrl(@ModelAttribute Page<Link> linkPage, @ModelAttribute Link link, @RequestParam Map<String, Object> queryParams, @RequestParam(required = false) Long[] ids, @RequestParam(required = false) Long[] orders,
            @RequestParam(required = false) Long[] deleteIds, @RequestParam(required = false) Long[] visableIds, HttpServletRequest request, HttpServletResponse response, Model model) {
        // 删除
        if (deleteIds != null && deleteIds.length > 0) {
            linkService.delete(deleteIds);
        }
        // 设置显示
        if (visableIds != null && visableIds.length > 0) {
            linkService.updateLinkVisable(true, visableIds);
        }
        // 设置不现实
        Long[] hiddenId = ArrayUtils.removeAll(ids, visableIds, Long.class);
        if (hiddenId != null && hiddenId.length > 0) {
            linkService.updateLinkVisable(false, hiddenId);
        }
        // 设置排序
        linkService.updateLinkOrders(ids, orders);
        return listLinks(linkPage, link, queryParams, request, response, model);
    }

}
